from archetypes.schemaextender.interfaces import ISchemaModifier
from Products.Archetypes.atapi import *
from zope.interface import implements, Interface
from zope.component import adapts, provideAdapter

from Products.Poi.interfaces import IIssue

from apkn.poiextender.config import ISSUE_FIELDS_TO_HIDE, ISSUE_FIELDS_TO_MODIFY

class IssueExtender(object):
    """Schema modifier to hide and rename some fields in the Issue content type.
    """
    
    adapts(IIssue)
    implements(ISchemaModifier)
        
    def fiddle(self, schema):
		#Hide certain fields
		for field in ISSUE_FIELDS_TO_HIDE:
			new_field = schema[field].copy()
			new_field.widget.visible = {'edit': 'invisible', 'view': 'invisible'}
			schema[field] = new_field
		
		#Rename fields
		for key in ISSUE_FIELDS_TO_MODIFY.keys():		
			new_field = schema[key].copy()
			if ISSUE_FIELDS_TO_MODIFY[key].has_key('label'):
				new_field.widget.label = ISSUE_FIELDS_TO_MODIFY[key]['label']
			if ISSUE_FIELDS_TO_MODIFY[key].has_key('description'):
				new_field.widget.description = ISSUE_FIELDS_TO_MODIFY[key]['description']							
			if ISSUE_FIELDS_TO_MODIFY[key].has_key('label_msgid'):
				new_field.widget.label_msgid = ISSUE_FIELDS_TO_MODIFY[key]['label_msgid']			
			if ISSUE_FIELDS_TO_MODIFY[key].has_key('description_msgid'):
				new_field.widget.description_msgid = ISSUE_FIELDS_TO_MODIFY[key]['description_msgid']			
			if ISSUE_FIELDS_TO_MODIFY[key].has_key('i18n_domain'):
				new_field.widget.i18n_domain = ISSUE_FIELDS_TO_MODIFY[key]['i18n_domain']
			if ISSUE_FIELDS_TO_MODIFY[key].has_key('allowable_content_types'):	
				new_field.allowable_content_types = ISSUE_FIELDS_TO_MODIFY[key]['allowable_content_types']											
			if ISSUE_FIELDS_TO_MODIFY[key].has_key('default_content_type'):	
				new_field.default_content_type = ISSUE_FIELDS_TO_MODIFY[key]['default_content_type']
			schema[key] = new_field				
		
    def __init__(self, context):
        self.context = context	
